#!/bin/bash
#----------HEADER-------------------------------------------------------------------|
#
#Directory of CDS convert to RANDOM.anon + md5
#
#----------END-HEADER---------------------------------------------------------------|

#>>>> Anon list
list_anon="/home/$USER/LISTA.anon"

#>>>> COLORS
end="\033[m"
red="\033[31;1m"
blue="\033[34;1m"


#>>> START SETTINGS
[ "$UID" = "0" ] && echo "Root execute? no." && exit 1
[ ! -e "$list_anon" ] && > $list_anon || cp "$list_anon" "${list_anon}.backup"


#>>> The magic Start here.
for file in * - *; do
  if [ -d "$file" ]; then
      random=$(echo "$RANDOM") #Generate random num
      while true; do
         if [[ $(grep "$random" "$list_anon") ]]; then  # random numeric exist in LISTA.anon?
            random=$(echo "$RANDOM")
         else
            break
         fi
      done # loop while
      echo ""
      echo -e "${red} $file ${end}"
      echo ""
      tar -cvJf "${random}.anon" "$file" #Create the anon folder compact (tar.xz)
      echo ""
      echo -e "${red}----OK${end} '$file' is >> ${random}.anon"
      echo ""
      md5sum "$random.anon" > "${random}.md5" #generate md5.
      echo ""
      echo -e "${blue}-----Remove directory${end} '$file'"
      echo ""
      rm -rf "$file" && echo -e "${red}----sucessfull.${end}"
      echo ""
      echo "${file}: ${random}" >> "$list_anon" # Send directory and key to LISTA.anon
      echo "${file}: ${random}" >> "${list_anon}.backup" # Send directory and key to backup
  fi
done

#Original list and list backup no have difference? remove.
diff "$list_anon" "${list_anon}.backup" > /dev/null
[ "$?" = "0" ] && rm "${list_anon}.backup"


